/**
 * Created by JetBrains WebStorm.
 * User: denysk
 * Date: 8/1/11
 * Time: 9:00 PM
 * To change this template use File | Settings | File Templates.
 */


Namespace("sketch.ui.common");

(function () {
    var common = sketch.ui.common,
        fwk = sketch.framework;

    common.StackPanel = klass(fwk.Container, {
        _callSuper:false,
        __type__:"ui.common.StackPanel",
        _constructor:function() {
            common.StackPanel.SuperConstructor.call(this, false);

            this.properties.createProperty("orientation", "Orientation", "horizontal")
                .possibleValues({horizontal: "Horizontal", vertical: "Vertical"});

            this.properties.createProperty("background", "Background color", null);
            this.properties.createProperty("autoWidth", "Auto width", false);
            this.properties.createProperty("autoHeight", "Auto height", false);
        },

        drawSelf : function(context) {
            var background = this.background();
            if (background) {
                context.save();
                context.fillStyle = background;
                context.fillRect(this.left(), this.top(), this.width(), this.height());
                context.restore();
            }

            common.StackPanel.Super.drawSelf.call(this, context);
        },

        background: function(value) {
            return this.properties.background(value);
        },

        canAccept:function(element) {
            return true;
        },

        orientation: function(value) {
            return this.properties.orientation(value);
        },
        autoWidth: function(value) {
            return this.properties.autoWidth(value);
        },
        autoHeight: function(value) {
            return this.properties.autoHeight(value);
        },

        _arrangeHorizontal: function(x, y, w, h) {
            var pos = this.padding().left();
            var topPadding = this.padding().top();
            var bottomPadding = this.padding().bottom();

            this.children.each(function(i, child) {
                var topSpace = topPadding + child.margin().top();
                var bottomSpace = bottomPadding + child.margin().bottom();

                pos += child.margin().left();
                child.left(pos);

                switch (child.verticalAlignment()) {
                    case "stretch":
                        child.top(topSpace);
                        child.height(h - topSpace - bottomSpace);
                        break;
                    case "top":
                        child.top(topSpace);
                        break;
                    case "center":
                        child.top(h / 2 - child.height() / 2);
                        break;
                    case "bottom":
                        child.top(h - bottomSpace - child.height());
                        break;
                }

                pos += child.width() + child.margin().right();
            });

            pos += this.padding().right();

            if (this.autoWidth()) {
                this.width(pos);
            }
        },
        _arrangeVertical: function(x, y, w, h) {
            var pos = this.padding().top();
            var leftPadding = this.padding().left();
            var rightPadding = this.padding().right();

            this.children.each(function(i, child) {
                var leftSpace = leftPadding + child.margin().left();
                var rightSpace = rightPadding + child.margin().right();

                pos += child.margin().top();
                child.top(pos);

                switch (child.horizontalAlignment()) {
                    case "stretch":
                        child.left(leftSpace);
                        child.width(w - leftSpace - rightSpace);
                        break;
                    case "left":
                        child.left(leftSpace);
                        break;
                    case "center":
                        child.left(w / 2 - child.width() / 2);
                        break;
                    case "right":
                        child.left(w - rightSpace - child.width());
                        break;
                }
                pos += child.height() + child.margin().bottom();
            });

            pos += this.padding().bottom();

            if (this.autoHeight()) {
                this.height(pos);
            }
        },

        arrange:function() {
            var length = this.children.count();
            if (length === 0) {
                return;
            }

            var w = this.width(),
                h = this.height(),
                x = this.left(),
                y = this.top();

            if (this.orientation() === "vertical") {
                this._arrangeVertical(x, y, w, h);
            }
            else {
                this._arrangeHorizontal(x, y, w, h);
            }
        }
    });
})();